@homedev/framework 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +40 -0
- package/dist/index.js +9 -3
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -43,6 +43,34 @@ export declare interface LoggerOptions {
|
|
|
43
43
|
logFunction: LogFunction;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
export declare class Markdown extends StringBuilder {
|
|
47
|
+
#private;
|
|
48
|
+
header(text: string, level?: number): this;
|
|
49
|
+
codeBlock(code: string, language?: string): this;
|
|
50
|
+
link(text: string, url: string): this;
|
|
51
|
+
table(headers: string[], rows: string[][]): this;
|
|
52
|
+
blockquote(text: string): this;
|
|
53
|
+
bold(text: string): this;
|
|
54
|
+
italic(text: string): this;
|
|
55
|
+
item(text: string): this;
|
|
56
|
+
list(items: string[]): this;
|
|
57
|
+
orderedList(items: string[], start?: number): this;
|
|
58
|
+
code(text: string): this;
|
|
59
|
+
horizontalRule(): this;
|
|
60
|
+
image(alt: string, url: string): this;
|
|
61
|
+
strikethrough(text: string): this;
|
|
62
|
+
highlight(text: string): this;
|
|
63
|
+
subscript(text: string): this;
|
|
64
|
+
superscript(text: string): this;
|
|
65
|
+
taskList(items: {
|
|
66
|
+
text: string;
|
|
67
|
+
checked: boolean;
|
|
68
|
+
}[]): this;
|
|
69
|
+
section(text: string): this;
|
|
70
|
+
endSection(): this;
|
|
71
|
+
setSectionLevel(level: number): this;
|
|
72
|
+
}
|
|
73
|
+
|
|
46
74
|
/**
|
|
47
75
|
* @public
|
|
48
76
|
*/
|
|
@@ -128,6 +156,16 @@ export declare interface SelectOptions {
|
|
|
128
156
|
optional?: boolean;
|
|
129
157
|
}
|
|
130
158
|
|
|
159
|
+
export declare class StringBuilder {
|
|
160
|
+
#private;
|
|
161
|
+
constructor(initial?: string | string[]);
|
|
162
|
+
append(...args: any[]): this;
|
|
163
|
+
appendLine(...args: any[]): this;
|
|
164
|
+
clear(): this;
|
|
165
|
+
isEmpty(): boolean;
|
|
166
|
+
toString(): string;
|
|
167
|
+
}
|
|
168
|
+
|
|
131
169
|
export { }
|
|
132
170
|
|
|
133
171
|
/* Global declarations */
|
|
@@ -190,6 +228,8 @@ declare global {
|
|
|
190
228
|
padBlock(indent: number, separator?: string, padder?: string): string;
|
|
191
229
|
tokenize(from: Record<string, unknown>, tokenizer?: RegExp): string;
|
|
192
230
|
stripIndent(): string;
|
|
231
|
+
toHtmlLink(path?: string): string;
|
|
232
|
+
toStringBuilder(): StringBuilder;
|
|
193
233
|
}
|
|
194
234
|
}
|
|
195
235
|
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
Array.prototype.findOrFail=function(n,r,
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
Array.prototype.findOrFail=function(n,r,t){let s=this.find(n,t);if(s===void 0)throw Error(r??"Element not found");return s};Array.flat=function(n){if(Array.isArray(n))return n.flat(2);else return[n]};Array.prototype.forEachAsync=async function(n){for(let r=0;r<this.length;r++)await n(this[r],r,this)};Array.prototype.groupBy=function(n){return this.reduce((r,t)=>{let s=n(t);if(!r[s])r[s]=[];return r[s].push(t),r},{})};Array.prototype.createInstances=function(n,...r){return this.map((t,s)=>new n(...r,t,s))};Array.prototype.mapAsync=async function(n){return Promise.all(this.map(n))};Array.prototype.nonNullMap=function(n){let r=[];for(let t=0;t<this.length;t++){let s=n(this[t],t,this);if(s!=null)r.push(s)}return r};Array.prototype.nonNullMapAsync=async function(n){let r=[];for(let t=0;t<this.length;t++){let s=await n(this[t],t,this);if(s!=null)r.push(s)}return r};Array.prototype.mergeAll=function(n){let r={};for(let t of this.toReversed())Object.merge(t,r,n);return r};Array.prototype.toObject=function(n,r){return Object.fromEntries(this.map((t)=>{let s=n(t),o=r?r(t):t;return[s,o]}))};Array.prototype.toObjectWithKey=function(n){return Object.fromEntries(this.map((r)=>[String(r[n]),r]))};Array.prototype.selectFor=function(n,r,t,s){let o=this.find(n,s);if(o===void 0)throw Error(t??"Element not found");return r(o)};Array.prototype.sortBy=function(n,r=!0){return this.slice().sort((t,s)=>{let o=n(t),e=n(s);if(o<e)return r?-1:1;if(o>e)return r?1:-1;return 0})};Array.prototype.unique=function(){return Array.from(new Set(this))};Array.prototype.uniqueBy=function(n){let r=new Set;return this.filter((t)=>{let s=n(t);if(r.has(s))return!1;else return r.add(s),!0})};var E;((i)=>{i[i.Explore=0]="Explore";i[i.SkipSiblings=1]="SkipSiblings";i[i.NoExplore=2]="NoExplore";i[i.ReplaceParent=3]="ReplaceParent";i[i.DeleteParent=4]="DeleteParent";i[i.MergeParent=5]="MergeParent";i[i.ReplaceItem=6]="ReplaceItem";i[i.DeleteItem=7]="DeleteItem"})(E||={});class T{action;by;skipSiblings;reexplore;constructor(n,r,t,s){this.action=n;this.by=r;this.skipSiblings=t;this.reexplore=s}static _explore=new T(0);static _noExplore=new T(2);static _skipSiblings=new T(1);static _deleteParent=new T(4);static ReplaceParent(n,r){return new T(3,n,void 0,r)}static SkipSiblings(){return T._skipSiblings}static Explore(){return T._explore}static NoExplore(){return T._noExplore}static DeleteParent(){return T._deleteParent}static MergeParent(n){return new T(5,n)}static ReplaceItem(n,r){return new T(6,n,r)}static DeleteItem(n){return new T(7,void 0,n)}}Object.navigate=async(n,r)=>{let t=new WeakSet,s=[],o=[],e=async(d,i,p)=>{let x=d===null,a=x?T.Explore():await r({key:d,value:i,path:s,parent:p,parents:o});if(i&&typeof i==="object"&&a.action===0){if(t.has(i))return a;if(t.add(i),!x)s.push(d),o.push(p);let m=i;while(await h(m,d,p))m=p[d];if(!x)o.pop(),s.pop()}return a},h=async(d,i,p)=>{let x=Object.keys(d),f=x.length;for(let a=0;a<f;a++){let g=x[a],c=d[g],m=await e(g,c,d),b=m.action;if(b===0||b===2)continue;if(b===6){if(d[g]=m.by,m.skipSiblings)return;continue}if(b===7){if(delete d[g],m.skipSiblings)return;continue}if(b===1)return;if(b===3){if(i===null)throw Error("Cannot replace root object");if(p[i]=m.by,m.reexplore)return!0;return}if(b===4){if(i===null)throw Error("Cannot delete root object");delete p[i];return}if(b===5)delete d[g],Object.assign(d,m.by)}};await e(null,n,null)};Object.find=async function(n,r){let t=[];return await Object.navigate(n,(s)=>{if(r([...s.path,s.key].join("."),s.value))t.push(s.value);return T.Explore()}),t};Object.merge=(n,r,t)=>{for(let s of Object.keys(r)){if(n[s]===void 0)continue;let o=r[s],e=n[s];if(typeof e!==typeof o||Array.isArray(e)!==Array.isArray(o)){let d=t?.mismatch?.(s,e,o,n,r);if(d!==void 0){r[s]=d;continue}throw Error(`Type mismatch: ${s}`)}let h=t?.mode?.(s,e,o,n,r)??r[".merge"]??"merge";switch(h){case"source":r[s]=e;continue;case"target":continue;case"skip":delete r[s],delete n[s];continue;case"merge":break;default:throw Error(`Unknown merge mode: ${h}`)}if(typeof e==="object")if(Array.isArray(e))o.push(...e);else{if(t?.navigate?.(s,e,o,n,r)===!1)continue;Object.merge(e,o,t)}else{if(e===o)continue;let d=t?.conflict?.(s,e,o,n,r);r[s]=d===void 0?o:d}}for(let s of Object.keys(n))if(r[s]===void 0)r[s]=n[s]};var O=/^\[([^=\]]*)([=]*)([^\]]*)\]$/,F=/^([^[\]]*)\[([^\]]*)]$/,w=(n,r,t,s)=>{if(n.startsWith("{")&&n.endsWith("}")){let o=n.substring(1,n.length-1);return Object.select(r,o,t)?.toString()}return s?n:void 0},U=(n,r,t,s)=>{let o=w(r,t,s);if(o)return Object.select(n,o,s);let e=O.exec(r);if(e){let[,d,i,p]=e,x=w(d.trim(),t,s,!0),f=w(p.trim(),t,s,!0);if(Array.isArray(n)&&(i==="="||i==="==")&&x)return n.find((a)=>a?.[x]==f)}let h=F.exec(r);if(h){let[,d,i]=h;return n?.[d]?.[i]}return n?.[r]};Object.select=(n,r,t)=>{let s=void 0,o=void 0,e=r??"",h=(f)=>{if(!f)return[f,!1];return f.endsWith("?")?[f.substring(0,f.length-1),!0]:[f,!1]},d=(f,a)=>{let[g,c]=h(a.pop());if(!g){if(t?.set!==void 0&&o!==void 0&&s!==void 0)s[o]=t.set;return f}let m=U(f,g,n,t);if(m===void 0){if(c||t?.optional||t?.defaultValue!==void 0)return;throw Error(`Unknown path element: "${g}" in "${e}"`)}return s=f,o=g,d(m,a)},i=e.splitNested(t?.separator??".","{","}"),p=void 0;if(i.length>0&&i[i.length-1].startsWith("?="))p=i.pop()?.substring(2);i.reverse();let x=d(n,i);if(x===void 0)return p??t?.defaultValue;return x};Object.deepClone=function(n){if(typeof structuredClone<"u")try{return structuredClone(n)}catch{}return JSON.parse(JSON.stringify(n))};Object.toList=function(n,r){return Object.entries(n).map(([t,s])=>({[r]:t,...s}))};Object.mapEntries=function(n,r){let s=Object.entries(n).map(([o,e])=>r(o,e));return Object.fromEntries(s)};global.AsyncFunction=Object.getPrototypeOf(async function(){}).constructor;global.defined=(n,r="Value is undefined")=>{if(n===void 0||n===null)throw Error(r);return n};String.prototype.tokenize=function(n,r=/\${([^}]+)}/g){return this.replace(r,(t,s)=>{let o=n[s];if(o===null||o===void 0)return"";if(typeof o==="string")return o;if(typeof o==="number"||typeof o==="boolean")return String(o);return String(o)})};String.prototype.toPascal=function(){return this.replace(/((?:[_ ]+)\w|^\w)/g,(n,r)=>r.toUpperCase()).replace(/[_ ]/g,"")};String.prototype.capitalize=function(){return this.replace(/((?:[ ]+)\w|^\w)/g,(n,r)=>r.toUpperCase())};String.prototype.toCamel=function(){return this.toPascal().replace(/((?:[ ]+\w)|^\w)/g,(n,r)=>r.toLowerCase())};String.prototype.toSnake=function(){return this.replace(/([a-z])([A-Z])/g,"$1_$2").replace(/\s+/g,"_")};String.prototype.toKebab=function(){return this.replace(/([a-z])([A-Z])/g,"$1-$2").replace(/\s+/g,"-")};String.prototype.changeCase=function(n){switch(n){case"upper":return this.toUpperCase();case"lower":return this.toLowerCase();case"pascal":return this.toPascal();case"camel":return this.toCamel();case"snake":return this.toSnake();case"kebab":return this.toKebab();default:throw Error(`Unsupported case type: ${n}`)}};String.prototype.splitWithQuotes=function(n=","){let r=[],t=this.length,s=n.length,o=0,e=(h)=>{if(h+s>t)return!1;for(let d=0;d<s;d++)if(this[h+d]!==n[d])return!1;return!0};while(o<t){while(o<t&&this[o]===" ")o++;if(o>=t)break;let h="",d=this[o]==='"'||this[o]==="'";if(d){let i=this[o++];while(o<t&&this[o]!==i)h+=this[o++];if(o<t)o++}else{while(o<t&&!e(o)){let i=this[o];if(i==='"'||i==="'"){h+=i,o++;while(o<t&&this[o]!==i)h+=this[o++];if(o<t)h+=this[o++]}else h+=this[o++]}h=h.trim()}if(h)r.push({value:h,quoted:d});if(e(o))o+=s}return r};String.prototype.splitNested=function(n,r,t){let s=0,o="",e=[];for(let h of this){if(o+=h,h===r)s++;if(h===t)s--;if(s===0&&h===n)e.push(o.slice(0,-1)),o=""}if(o)e.push(o);return e};String.prototype.toStringBuilder=function(){return new u(this.split(`
|
|
2
|
+
`))};String.prototype.padBlock=function(n,r=`
|
|
3
|
+
`,t=" "){let s=t.repeat(n);return this.split(r).map((o)=>s+o).join(r)};String.prototype.stripIndent=function(){let n=this.split(/\r?\n/),r=n.filter((s)=>s.trim().length>0).map((s)=>/^[ \t]*/.exec(s)?.[0].length??0),t=r.length>0?Math.min(...r):0;if(t===0)return this;return n.map((s)=>s.slice(t)).join(`
|
|
4
|
+
`)};String.prototype.toHtmlLink=function(n){return`<a href="${n??this.toString()}">${this.toString()}</a>`};function v(n,r="Value is undefined"){if(n===void 0||n===null)throw Error(r);return n}class L{options;constructor(n){this.options={level:"INFO",showClass:!1,levels:["DEBUG","VERBOSE","INFO","WARN","ERROR"],timeStamp:!1,timeOptions:{second:"2-digit",minute:"2-digit",hour:"2-digit",day:"2-digit",month:"2-digit",year:"2-digit"},logFunction:(r,...t)=>{switch(r){case"ERROR":console.error(...t);break;case"WARN":console.warn(...t);break;case"DEBUG":console.debug(...t);break;case"VERBOSE":console.log(...t);break;default:console.log(...t)}},...n}}write(n,...r){if(this.options.level){let o=this.options.levels.indexOf(this.options.level)??-1,e=this.options.levels.indexOf(n);if(e===-1)throw Error(`Unknown log level: ${n}`);if(e<o)return}let t=[],s=[];if(this.options.timeStamp)s.push(new Date().toLocaleString(this.options.timeLocale,this.options.timeOptions));if(this.options.showClass)s.push(n);if(s.length>0)t.push(`[${s.join(" ")}]`);t.push(...r),this.options.logFunction(n,...t)}info(...n){this.write("INFO",...n)}error(...n){this.write("ERROR",...n)}warn(...n){this.write("WARN",...n)}debug(...n){this.write("DEBUG",...n)}verbose(...n){this.write("VERBOSE",...n)}}class u{#n=[];constructor(n){if(n)if(Array.isArray(n))this.#n.push(...n);else this.#n.push(n)}append(...n){return this.#n.push(...n),this}appendLine(...n){return this.append(...n.map((r)=>{if(r===null||r===void 0)return`
|
|
5
|
+
`;return`${r.toString()}
|
|
6
|
+
`})),this}clear(){return this.#n.length=0,this}isEmpty(){return this.#n.length===0}toString(){return this.#n.join("")}}class $ extends u{header(n,r=1){return this.appendLine(`${"#".repeat(r)} ${n}`)}codeBlock(n,r=""){return this.appendLine(`\`\`\`${r}
|
|
7
|
+
${n}
|
|
8
|
+
\`\`\``)}link(n,r){return this.append(`[${n}](${r})`)}table(n,r){let t=n.map((e,h)=>{let d=e.length;return r.forEach((i)=>{if(i[h])d=Math.max(d,i[h].length)}),d}),s=n.map((e,h)=>e.padEnd(t[h]));this.appendLine("| "+s.join(" | ")+" |");let o=t.map((e)=>"-".repeat(e));return this.appendLine("| "+o.join(" | ")+" |"),r.forEach((e)=>{let h=e.map((d,i)=>d.padEnd(t[i]));this.appendLine("| "+h.join(" | ")+" |")}),this}blockquote(n){let r=n.split(`
|
|
9
|
+
`);this.appendLine("> "+r[0]);for(let t=1;t<r.length;t++)this.appendLine("> "+r[t]);return this}bold(n){return this.append(`**${n}**`)}italic(n){return this.append(`*${n}*`)}item(n){return this.appendLine(`- ${n}`)}list(n){return n.forEach((r)=>this.item(r)),this}orderedList(n,r=1){return n.forEach((t,s)=>this.appendLine(`${(s+r).toString()}. ${t}`)),this}code(n){return this.append(`\`${n}\``)}horizontalRule(){return this.appendLine("---")}image(n,r){return this.appendLine(``)}strikethrough(n){return this.append(`~~${n}~~`)}highlight(n){return this.append(`==${n}==`)}subscript(n){return this.append(`~${n}~`)}superscript(n){return this.append(`^${n}^`)}taskList(n){return n.forEach((r)=>this.appendLine(`- [${r.checked?"x":" "}] ${r.text}`)),this}#n=0;section(n){return this.#n++,this.header(n,this.#n)}endSection(){if(this.#n>0)this.#n--;return this}setSectionLevel(n){return this.#n=n,this}}export{v as defined,u as StringBuilder,T as NavigateResult,E as NavigateAction,$ as Markdown,L as Logger};
|